# -*- coding: UTF-8 -*-

##########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

from flask import Blueprint
from flask import request
import utils.pgsql.pgcvm
import utils.pgsql.pgcase
import utils.pgsql.pgdevice
import utils.pgsql.pgadmin
import requests
from utils.wraps import token, urlcost
import utils.message.response as response
import json

### 服务器管理
#  Server management

cvm_module = Blueprint("base_cvm", __name__, url_prefix="/api/v1/cvm")


@cvm_module.route("/admin", methods=["GET"])
@token.login_required
@urlcost.cost_count_wraps
def cvm_all():

    cvm_obj = utils.pgsql.pgcvm.PgCvm()
 
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])
    if userInfo["role"] in [1,2,3]:
        return response.make_response_success(cvm_obj.all())
    else:
        return response.make_response_401()


@cvm_module.route("/admin", methods=["POST"])
@token.login_required
@urlcost.cost_count_wraps
def cvm_add():

    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo["role"] == 1:
        obj = utils.pgsql.pgcvm.PgCvm()
        info = json.loads(request.data)
        # info.pop("cvmId")
        # print(info)

        res = obj.add(info)
        if res == {}:
            return response.make_response_1505()
        return response.make_response_success(res)
    else:
        return response.make_response_401()


@cvm_module.route("/<int:cvmId>", methods=["GET"])
@token.login_required
@urlcost.cost_count_wraps
def cvm_get(cvmId):
   
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])
    if userInfo["role"] == 1:
        obj = utils.pgsql.pgcvm.PgCvm()
        return response.make_response_success(obj.get(cvmId))
    else:
        return response.make_response_401()


@cvm_module.route("/<int:cvmId>", methods=["PUT"])
@token.login_required
@urlcost.cost_count_wraps
def cvm_put(cvmId):
  
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])
    if userInfo["role"] in [1]:
        obj = utils.pgsql.pgcvm.PgCvm()
        dic = json.loads(request.data)

        if obj.modify(dic, "cvmId", cvmId):
            return response.make_response_success_no_data()
        else:
            return response.make_response_1000()
    else:
        return response.make_response_401()


@cvm_module.route("/state", methods=["PUT"])
@token.login_required
@urlcost.cost_count_wraps
def cvm_enable():
 
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo["role"] == 1:
        dic = json.loads(request.data)
        obj = utils.pgsql.pgcvm.PgCvm()
        if obj.updateState(dic["state"], dic["cvmId"]):
            return response.make_response_success_no_data()
        else:
            return response.make_response_1000()
    else:
        return response.make_response_401()


@cvm_module.route("/<int:cvmId>", methods=["DELETE"])
@token.login_required
@urlcost.cost_count_wraps
def cvm_delete(cvmId):
 
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])
    if userInfo["role"] == 1 :
        obj = utils.pgsql.pgcvm.PgCvm()
        if obj.delete(cvmId):
            return response.make_response_success_no_data()
        else:
            return response.make_response_1000()
    else:
        return response.make_response_401()

